import Vue from 'vue'
import Router from 'vue-router'

// Routes Components
import systemRoute from './modules/system'
import authorizeRoute from './modules/authorize'
import appRoute from './modules/app'
import activityRoute from './modules/activity'
import feedbackRoute from './modules/feedback'
import passportRoute from './modules/passport'
import shopRoute from './modules/shop'
import userRoute from './modules/user'
import memberRoute from './modules/member'
import pageRoute from './modules/page'
Vue.use(Router)

let routes = new Set([
  ...[{
    path: '/',
    redirect: '/home'
  }],
  ...authorizeRoute,
  ...appRoute,
  ...activityRoute,
  ...feedbackRoute,
  ...passportRoute,
  ...shopRoute,
  ...userRoute,
  ...memberRoute,
  ...pageRoute,
  ...systemRoute
])
console.log(routes)
// add route path
routes.forEach(route => {
  route.path = route.path || '/' + (route.name || '')
})

const router = new Router({
  // mode: 'history',
  // base: process.env.assetsRoot,
  routes: routes
})

router.beforeEach((to, from, next) => {
  // if (!sessionStorage.getItem('accessToken2')) {
  //   next({
  //     path: '/authorize',
  //     query: {
  //       redirect: to.fullPath
  //     }
  //   })
  //   return
  // }
  let cc = '12' // detecteDevice('and')

  console.log('to.name=============')
  console.log(cc)
  console.log(to)
  console.log(to.name)
  console.log('to.name=============')
  if (/micromessenger/i.test(navigator.userAgent) && to.name !== 'wechat/auth') {
    // let wxUserInfo = localStorage.getItem('wxUserInfo')
    let wxUserInfo = sessionStorage.getItem('wxUserInfo')
    // let authing = localStorage.getItem('authing')
    console.log('wxUserInfo=============')
    console.log(wxUserInfo)
    console.log('wxUserInfo=============')
    if (!wxUserInfo) {
      // localStorage.setItem('authing', '2')
      // 保存当前路由地址，授权后还会跳到此地址
      // sessionStorage.setItem('wxRedirectUrl', to.fullPath)

      // new Promise((resolve, reject) => {
      //   jsBridge.loadJumpWechat(res => {
      //     resolve(res)
      //   })
      // }).then((data) => {
      //   console.log(data)
      // })

      // // 请求微信授权,并跳转到 /WxAuth 路由
      let appId = 'wx1b4b2f622534cd9e'
      let redirectUrl = encodeURIComponent(window.location.origin + '/#/wechat/auth')
      let wechatAuthUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirectUrl}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
      // 判断是否为正式环境
      if (process.env.NODE_ENV === 'development') {
        wechatAuthUrl = `http://open.wechatauth.com/authorize.php?appid=${appId}&redirect_uri=${redirectUrl}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
      }
      console.log('wechatAuthUrl=============')
      console.log(wechatAuthUrl)
      console.log('wechatAuthUrl=============')
      // window.location.href = wechatAuthUrl
      // alert(wechatAuthUrl)
      next()
    } else {
      if (to.meta && to.meta.title) {
        document.title = to.meta.title
      }
      next()
    }
  } else {
    if (to.meta && to.meta.title) {
      document.title = to.meta.title
    }
    next()
  }
})

export default router
